package com.iurac.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iurac.domain.Borrow;
import com.iurac.domain.BorrowHistory;
import com.iurac.exception.BorrowException;
import com.iurac.service.BorrowService;
import com.iurac.uitl.DateTimeUtil;
import com.iurac.uitl.UUIDUtil;
import com.iurac.vo.BorrowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BorrowController {

    @Autowired
    BorrowService borrowService;

    @RequestMapping("/borrow/getBorrowByCondition.do")
    @ResponseBody
    public Map<String, Object> getBorrowByCondition(@RequestParam(value="page") int page, @RequestParam("limit") int limit,
                                       @RequestParam("username") String username, @RequestParam(value="userId") String userId,
                                       @RequestParam(value="title") String title) throws IOException {
        List<BorrowVo> borrowVoList = borrowService.getBorrowByCondition(title,username,userId);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",borrowVoList.size());
        map.put("data",borrowVoList);

        return  map;
    }

    @RequestMapping("/borrow/getBorrowByUid.do")
    @ResponseBody
    public Map<String, Object> getBorrowByUid(@RequestParam(value="page") int page, @RequestParam("limit") int limit,
                                       @RequestParam(value="userId") String userId) throws IOException {
        List<BorrowVo> borrowVoList = borrowService.getBorrowByUid(userId);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",borrowVoList.size());
        map.put("data",borrowVoList);

        return  map;
    }

    @RequestMapping("/borrow/getBorrowByAccount.do")
    @ResponseBody
    public Map<String, Object> getBorrowByAccount(@RequestParam(value="page") int page, @RequestParam("limit") int limit,
                                       @RequestParam(value="account") String account) throws IOException {
        List<BorrowVo> borrowVoList = borrowService.getBorrowByAccount(account);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",borrowVoList.size());
        map.put("data",borrowVoList);

        return  map;
    }

    @RequestMapping("/borrow/getBorrowHistoryByCondition.do")
    @ResponseBody
    public Map<String, Object> getBorrowHistoryByCondition(@RequestParam(value="page") int page, @RequestParam("limit") int limit,
                                       @RequestParam("username") String username, @RequestParam(value="userId") String userId,
                                       @RequestParam(value="title") String title, @RequestParam(value="startDate") String startDate,
                                       @RequestParam(value="endDate",required = false) String endDate) throws IOException {
        List<BorrowHistory> borrowHistoryList = borrowService.getBorrowHistoryByCondition(title,username,userId,startDate,endDate);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",borrowHistoryList.size());
        map.put("data",borrowHistoryList);
        return  map;
    }

    @RequestMapping("/borrow/deleteBorrowById.do")
    @ResponseBody
    public Map<String, Object> deleteBorrowById(@RequestParam(value="id") String id) throws IOException {
        boolean success;
        try {
            success = borrowService.deleteBorrowById(id);
        } catch (BorrowException e) {
            e.printStackTrace();
            success = false;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success",success);

        return  map;
    }

    @RequestMapping("/borrow/deleteBorrowHistoryById.do")
    @ResponseBody
    public Map<String, Object> deleteBorrowHistoryById(@RequestParam(value="id") String id) throws IOException {
        boolean success;
        try {
            success = borrowService.deleteBorrowHistoryById(id);
        } catch (BorrowException e) {
            e.printStackTrace();
            success = false;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success",success);

        return  map;
    }

    @RequestMapping("/borrow/borrowBook.do")
    @ResponseBody
    public Map<String, Object> borrowBook(Borrow borrow) throws IOException {
        borrow.setId(UUIDUtil.getUUID());
        borrow.setBorrowDate(DateTimeUtil.getSysTime());
        boolean success;
        String msg = "";
        Map<String, Object> map = new HashMap<>();
        try {
            success = borrowService.borrowBook(borrow);
        } catch (BorrowException e) {
            e.printStackTrace();
            success = false;
            msg = e.getMessage();
        }
        map.put("success",success);
        map.put("msg",msg);

        return  map;
    }

    @RequestMapping("/borrow/returnBook.do")
    @ResponseBody
    public Map<String, Object> returnBook(@RequestParam("borrowId") String bid, BorrowHistory borrowHistory) throws IOException {
        borrowHistory.setId(UUIDUtil.getUUID());
        borrowHistory.setReturnDate(DateTimeUtil.getSysTime());
        boolean success;
        try {
            success = borrowService.returnBook(bid,borrowHistory);
        } catch (BorrowException e) {
            e.printStackTrace();
            success = false;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success",success);

        return  map;
    }

    @RequestMapping("/borrow/renewBook.do")
    @ResponseBody
    public Map<String, Object> renewBook(@RequestParam("id") String id, @RequestParam("renewDay") String renewDay) throws IOException {
        boolean success;
        String msg = "";
        try {
            success = borrowService.renewBook(id,renewDay);
        } catch (BorrowException e) {
            e.printStackTrace();
            success = false;
            msg = e.getMessage();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("success",success);
        map.put("msg",msg);

        return  map;
    }

}
